﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using FPModel;
using WSFacebookPoster.proxyFacebook;
using System.Data.Objects;

namespace WSFacebookPoster
{
    public class FacebookQuery
    {
        private static readonly log4net.ILog _logger = LogManager.GetLogger(typeof(FacebookQuery));
       
        public void Test()
        {
            log4net.Config.XmlConfigurator.Configure();
            FBWebService proxy = new FBWebService();
            List<PostMessage> messages = new List<PostMessage>();

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug("windows service test method entered");
            }

            using (FBPosterEntities context = new FBPosterEntities())
            {
                DateTime fromDate = DateTime.Now.AddMinutes(-6);
                DateTime tillDate = DateTime.Now.AddMinutes(6);
                var query =
                    from user in context.FBUsers
                    from pages in context.FBPages
                    from post in context.FBPagePosts
                    orderby post.PostDate descending
                    where 
                        user.ID == pages.UserID
                        && pages.ID == post.PageID
                        && post.PostedDate > fromDate
                        && post.PostedDate <= tillDate
                        && post.IsPosted == false
                    select new
                    {
                        UserToken = user.UserToken,
                        PageToken = pages.PageToken,
                        Name = post.Name,
                        Description = post.Description,
                        Message = post.Message,
                        PageId = pages.PageNo,
                        Link = post.Link,
                        ID = post.ID
                    };

                foreach (var post in query)
                {
                    string pst = String.Format("{0} | {1} | {2}", post.UserToken, post.Name, post.Message);
                    _logger.Info(pst);

                    PostMessage message = new PostMessage();
                    message.AccessToken = post.PageToken;
                    message.PageId = post.PageId;
                    message.Name = post.Name;
                    message.Link = post.Link;
                    message.Description = post.Description;
                    message.Message = post.Message;
                    message.PostMessageId = post.ID;
                    messages.Add(message);

                    proxy.PostMessageToPage(message);

                    // Update IsPosted status
                    var pagePostEdit = (from c in context.FBPagePosts where c.ID == message.PostMessageId select c).First();
                    pagePostEdit.IsPosted = true;
                    
                }
                context.SaveChanges();
            }

            if (_logger.IsInfoEnabled)
            {
                _logger.InfoFormat("windows service test method finished");
            }
        }

        public void GetUserInformation()
        {
            log4net.Config.XmlConfigurator.Configure();
            FBWebService proxy = new FBWebService();
            
            List<PostMessage> messages = new List<PostMessage>();
            PostMessage message = new PostMessage();
            message.AccessToken = "AAADuFysZCJxcBAJupBZB5fXQTr3XNr8Til7GACbftXZBRbt5zc3MCwjTCGYm6M3QzxrHLbcrty1fa7H8drUBapovF68fHUZD";
            message.PageId = "290575864329866";
            message.Name = "Google";
            message.Link = "www.google.com.tr";
            message.Description = "Search engine";
            message.Message = "Most used search engine in the world";
            messages.Add(message);

            PostMessage message2 = new PostMessage();
            message2.AccessToken = "AAADuFysZCJxcBAJupBZB5fXQTr3XNr8Til7GACbftXZBRbt5zc3MCwjTCGYm6M3QzxrHLbcrty1fa7H8drUBapovF68fHUZD";
            message2.PageId = "290575864329866";
            message2.Name = "Google";
            message2.Link = "www.google.com.tr";
            message2.Description = "Search engine";
            message2.Message = "Most used search engine in the world";
            messages.Add(message2);

            _logger.Info("Checking for user information for facebook...");
            proxy.PostMessagesToPage(messages.ToArray());

            
            using (FBPosterEntities context = new FBPosterEntities())
            {
                //var result = from c in context.FBUsers
                //             select c;
                //ObjectSet<FBPagePost> products = context.FBPagePosts;

                var posts = from p in context.FBPagePosts
                            orderby p.PostDate descending
                            where p.IsPosted == false  //&& p.PostDate > DateTime.Now
                            select p;

                foreach (var post in posts)
                {
                    string pst = String.Format("{0} | {1} | {2}", post.Name, post.Description, post.Message);
                    _logger.Info(pst);
                }

                //var pages = from page in context.FBPages
                //            where page.UserID == 12
                //            select page;

                //_logger.Info("Total count of users : " + result.ToList().Count);
                _logger.Info("Total count of post : " + posts.ToList().Count);
                //_logger.Info("Total count of pages : " + pages.ToList().Count);
            }
        }
    }
}
